package com.lst.servlet;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.SportRecordsReqPara;
import com.lst.entity.response.MedalResPara;
import com.lst.model.MstDiscuss;
import com.lst.model.OnlineCourse;
import com.lst.model.SportRecords;
import com.lst.service.ClientNumService;
import com.lst.service.FocusService;
import com.lst.service.MstDiscussService;
import com.lst.service.OnlineCourseService;
import com.lst.service.PlanService;
import com.lst.service.SportRecordsService;
import com.lst.utils.CommFunUtils;
import com.lst.utils.RequestUtils;


/**
 * @ClassName: MedalServlet
 * @Description: 勋章接口
 * 
 */
public class MedalServlet extends BaseServlet {

	/**
	 * @Fields serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private SportRecordsService sportRecordsService = ctx.getBean(SportRecordsService.class);
	
	private PlanService planService = ctx.getBean(PlanService.class);
	
	private OnlineCourseService onlineCourseService = ctx.getBean(OnlineCourseService.class);
	
	private FocusService focusService = ctx.getBean(FocusService.class);
	
	private MstDiscussService mstDiscussService = ctx.getBean(MstDiscussService.class); 
	
	private ClientNumService clientNumService = ctx.getBean(ClientNumService.class);
	
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		MedalResPara resPara = new MedalResPara();

		try {
			SportRecordsReqPara reqPara = getReqPara(request);
		
		if(isCheckField(reqPara,resPara)){
			Integer userid = Integer.valueOf(reqPara.getUserid());
			
			List<SportRecords> sportList=sportRecordsService.countByUser4(userid);
			
			Map<String,Object> map=new HashMap<String,Object>();
			//连续型勋章
			Boolean lxNum7=false;    //连续训练7天
			Boolean lxNum21=false;    //连续训练21天
			Boolean lxNum100=false;  //连续训练100天
			Boolean lxNum365=false;  //连续训练100天
			if(sportList!=null && sportList.size()>0){
				for(SportRecords sportRecords:sportList){
					int lxNum=sportRecords.getTotalNum();
					if(lxNum>=7){
						lxNum7=true;
					}
					if(lxNum>=21){
						lxNum21=true;
					}
					if(lxNum>=100){
						lxNum100=true;
					}
					if(lxNum>=365){
						lxNum365=true;
					}
				}
				
			}
			map.put("lxNum7", lxNum7);
			map.put("lxNum21", lxNum21);
			map.put("lxNum100", lxNum100);
			map.put("lxNum365", lxNum365);
			
			//首次型勋章
			Boolean planNum1=false;   //完成一次计划
			Boolean planNum4=false;   //完成4次计划
			Boolean planNum16=false;  //完成16次计划
			int planNum=planService.countByUser(userid);
			if(planNum>=1){
				planNum1=true;
			}
			if(planNum>=4){
				planNum4=true;
			}
			if(planNum>=16){
				planNum16=true;
			}
			
			map.put("planNum1", planNum1);
			map.put("planNum4", planNum4);
			map.put("planNum16", planNum16);
			
			//专注勋章
			Boolean quanNum5=false;//完成5次拳法课程
			Boolean tuiNum5=false;//完成5次腿法课程
			Boolean diNum5=false;//完成5次地面课程
			int num1=0;
			int num2=0;
			int num3=0;
			if(sportList!=null && sportList.size()>0){
				for(SportRecords sportRecords:sportList){
					OnlineCourse onlineCourse=onlineCourseService.selectByPrimaryKey(sportRecords.getCourseId());
					int num=onlineCourse.getKfClassType();
					if(num==389){
						num1++;
					}
					if(num==390){
						num2++;
					}
					if(num==391){
						num3++;
					}
				}
				if(num1>=5){
					quanNum5=true;
				}
				if(num2>=5){
					tuiNum5=true;
				}
				if(num3>=5){
					quanNum5=true;
				}
			}

			map.put("quanNum5", quanNum5);
			map.put("tuiNum5", tuiNum5);
			map.put("diNum5", diNum5);
			//获得1万粉丝关注勋章
			Boolean fanNum=false;//获得1万粉丝关注
			int totalFansNum=focusService.countFansTotal(userid);
			if(totalFansNum>=10000){
				fanNum=true;
			}
		
			map.put("fanNum", fanNum);
			
			//麒麟才子勋章，获得赞超过一千
			Boolean qlczNum=false;//获得赞超过一千
			Map<String, Object> dymap = new HashMap<String, Object>();
			dymap.put("enabled", true);
			dymap.put("isDeleted", false);
			dymap.put("userid",userid);
			List<MstDiscuss> mstDiscussList=mstDiscussService.queryListByMap(dymap);
			if(mstDiscussList!=null && mstDiscussList.size()>0){
				for(MstDiscuss mstDiscuss:mstDiscussList){
					//点赞量
					Integer praisenum = CommFunUtils.opNum(null, mstDiscuss.getId(), CommCode.SYSTEM_CARESEL_RANGE_DISCUSS, CommCode.SYSTEM_COUNT_PRAISE, clientNumService);
					if(praisenum>=1000){
						qlczNum=true;
						break;
					}
				}
			}
			map.put("qlczNum", qlczNum);
		
			
			resPara.setMedalMap(map);
			resPara.setCode(CommCode.M_Y000000);
			resPara.setMessage(CommCode.M_Y000001);
		  }
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);
			logger.error("doPost error: ", e);
		}
		
		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);
	//		logger.info("ClientDiscussServlet jsonResult:" + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

//			logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public SportRecordsReqPara getReqPara(HttpServletRequest request) {

		SportRecordsReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new SportRecordsReqPara());

			reqPara = (SportRecordsReqPara) baseRequest.clone();

			String userid = request.getParameter("userid");
	
	//		logger.info("reqPara userid:" + userid);

	        reqPara.setUserid(userid);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		SportRecordsReqPara cdReqPara = (SportRecordsReqPara) reqPara;
		BaseResponse cdResPara = (BaseResponse) resPara;

        if (StringUtils.isEmpty(cdReqPara.getUserid())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000002);
		}  else {
			flag = true;
		}

		return  flag;
	}

}
